package com.example.hookapp.base;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {
    protected Activity activity;
    protected abstract int provideContentViewId();

    protected View mView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int layoutId = provideContentViewId();
        if (layoutId == 0) {
            if ((mView = provideContentView(container)) == null) {
                throw new RuntimeException("please provide a layout id !!");
            }
        } else {
            mView = inflater.inflate(layoutId, null);
        }
        mView.setClickable(true);
        ButterKnife.bind(this, mView);
        return mView;
    }

    protected View provideContentView(ViewGroup container) {
        return null;
    }

    protected abstract void initViews(Bundle savedInstanceState);

    protected abstract void initData(Bundle savedInstanceState);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        EventBus.getDefault().register(this);
        initViews(savedInstanceState);
        initData(savedInstanceState);
    }
}
